package cn.com.bo.util.crypto;

import javax.crypto.Mac;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  

import cn.com.bo.util.PropUtils;
  
public class HMACSHA1 {  
  
    private static final String MAC_NAME = "HmacSHA1";    
    private static final String ENCODING = "UTF-8";    
      
    /* 
     * 展示了一个生成指定算法密钥的过程 初始化HMAC密钥  
     * @return  
     * @throws Exception 
     *  
      public static String initMacKey() throws Exception { 
      //得到一个 指定算法密钥的密钥生成器 
      KeyGenerator KeyGenerator keyGenerator =KeyGenerator.getInstance(MAC_NAME);  
      //生成一个密钥 
      SecretKey secretKey =keyGenerator.generateKey(); 
      return null; 
      } 
     */  
      
    /**  
     * 使用 HMAC-SHA1 签名方法对对encryptText进行签名  
     * @param encryptText 被签名的字符串  
     * @param encryptKey  密钥  
     * @return  
     * @throws Exception  
     */    
    public static String HmacSHA1(String encryptText, String encryptKey) throws Exception     
    {           
        return new String(HmacSHA1Encrypt(encryptText,encryptKey));    
    }  
    
    public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception     
    {           
        byte[] data=encryptKey.getBytes(ENCODING);  
        //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称  
        SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);   
        //生成一个指定 Mac 算法 的 Mac 对象  
        Mac mac = Mac.getInstance(MAC_NAME);   
        //用给定密钥初始化 Mac 对象  
        mac.init(secretKey);    
          
        byte[] text = encryptText.getBytes(ENCODING);    
        //完成 Mac 操作   
        return mac.doFinal(text);    
    }  
    
	public static String getSignature(String data, String key) throws Exception {
		byte[] keyBytes = key.getBytes();
		SecretKeySpec signingKey = new SecretKeySpec(keyBytes, MAC_NAME);
		Mac mac = Mac.getInstance(MAC_NAME);
		mac.init(signingKey);
		byte[] rawHmac = mac.doFinal(data.getBytes());
		StringBuilder sb = new StringBuilder();
		for (byte b : rawHmac) {
			sb.append(byteToHexString(b));
		}
		return sb.toString();
	}
	
	public static String getBase64Signature(String data, String key) throws Exception {
		byte[] keyBytes = key.getBytes();
		SecretKeySpec signingKey = new SecretKeySpec(keyBytes, MAC_NAME);
		Mac mac = Mac.getInstance(MAC_NAME);
		mac.init(signingKey);
		byte[] rawHmac = mac.doFinal(data.getBytes());
		return Base64Util.getBase64(rawHmac);
	}
	
	private static String byteToHexString(byte ib) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
				'b', 'c', 'd', 'e', 'f' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0f];
		ob[1] = Digit[ib & 0X0F];
		String s = new String(ob);
		return s;
	}    
    
    public static void main(String[] args) {
    	try {
			System.out.println(getBase64Signature("POST&http%3A%2F%2Fapi.ad.sohu.com%2Fexchange%2Fcustomer%2Fcreate&auth_consumer_key%3D2b2262e8617339392d59470fcf74adab%26auth_nonce%3D761df2f417384ba3b09a0ae6c2a2469b%26auth_signature_method%3DHMAC-SHA1%26auth_timestamp%3D1499225989%26customer_name%3D%E6%B5%8B%E8%AF%95ggz1%26phone_number%3D1234567890", PropUtils.get("sohu.secret")));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}  